#include "loginwidget.h"
#include "ui_loginwidget.h"
#include "registerwidget.h"

#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QTcpSocket>
#include <QByteArray>
#include <cstring>
#include <QDialog>
#include <QCryptographicHash>


LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget),
    socket(new QTcpSocket(this)) // 初始化套接字
{
    ui->setupUi(this);

    setWindowTitle("QQ 登录界面");
    //resize(300, 200);
    setFixedSize(350, 265);
    //设置图标
    QIcon icon(QString(":/new/prefix1/images/QQ.png"));
    this->setWindowIcon(icon);
    // 布局设置
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 用户名输入
    QLabel *userLabel = new QLabel("用户名:", this);
    userLabel->setStyleSheet("QLabel {"
                                 "color: #333333;"
                                 "font-size: 16px;"
                                 "font-weight: bold;"
                                 "margin-bottom: 5px;"
                                 "}"); // 标签文字颜色和字体加粗
    userLineEdit = new QLineEdit(this);
    userLineEdit->setPlaceholderText("请输入用户名");
    userLineEdit->setStyleSheet("QLineEdit {"
                                    "background-color: #f0f0f0;"
                                    "border-radius: 5px;"
                                    "border: 1px solid #ccc;"
                                    "padding: 10px;"
                                    "font-size: 14px;"
                                    "}"
                                    "QLineEdit:focus {"
                                    "border: 1px solid #2196F3;"
                                    "background-color: #ffffff;"
                                    "}"); // 获得焦点时的样式变化

    // 密码输入
    QLabel *passwordLabel = new QLabel("密码:", this);
    passwordLineEdit = new QLineEdit(this);
    passwordLineEdit->setEchoMode(QLineEdit::Password);
    passwordLineEdit->setPlaceholderText("请输入密码");
    passwordLineEdit->setStyleSheet("QLineEdit {"
                                        "background-color: #f0f0f0;"
                                        "border-radius: 5px;"
                                        "border: 1px solid #ccc;"
                                        "padding: 10px;"
                                        "font-size: 14px;"
                                        "}"
                                        "QLineEdit:focus {"
                                        "border: 1px solid #2196F3;"
                                        "background-color: #ffffff;"
                                        "}"); // 获得焦点时的样式变化

    // 记住密码复选框
    rememberCheckBox = new QCheckBox("记住密码", this);

    // 忘记密码和注册按钮
    QPushButton *forgotPasswordButton = new QPushButton("忘记密码", this);
    forgotPasswordButton->setStyleSheet("QPushButton {"
                                        "background-color: #2196F3;"
                                        "color: white;"
                                        "border-radius: 5px;"
                                        "padding: 10px 20px;"
                                        "font-size: 14px;"
                                        "border: none;"
                                        "}"
                                        "QPushButton:hover {"
                                        "background-color: #0b7dda;"
                                        "}"); // 鼠标悬停时下划线效果

    registerButton = new QPushButton("注册", this);
    // 注册按钮
    registerButton->setStyleSheet("QPushButton {"
                                  "background-color: #2196F3;"
                                  "color: white;"
                                  "border-radius: 5px;"
                                  "padding: 10px 20px;"
                                  "font-size: 14px;"
                                  "border: none;"
                                  "}"
                                  "QPushButton:hover {"
                                  "background-color: #0b7dda;"
                                  "}"); // 鼠标悬停时颜色变化

    // 登录和退出按钮
    loginButton = new QPushButton("登录", this);
    loginButton->setStyleSheet("QPushButton {"
                               "background-color: #4CAF50;"
                               "color: white;"
                               "border-radius: 5px;"
                               "padding: 10px 20px;"
                               "font-size: 14px;"
                               "border: none;"
                               "}"
                               "QPushButton:hover {"
                               "background-color: #45a049;"
                               "}"); // 鼠标悬停时颜色变化
    QPushButton *exitButton = new QPushButton("退出", this);
    exitButton->setStyleSheet("QPushButton {"
                              "background-color: #f44336;"
                              "color: white;"
                              "border-radius: 5px;"
                              "padding: 10px 20px;"
                              "font-size: 14px;"
                              "border: none;"
                              "}"
                              "QPushButton:hover {"
                              "background-color: #e53935;"
                              "}"); // 鼠标悬停时颜色变化

    // 布局管理
    mainLayout->addWidget(userLabel);
    mainLayout->addWidget(userLineEdit);
    mainLayout->addWidget(passwordLabel);
    mainLayout->addWidget(passwordLineEdit);
    mainLayout->addWidget(rememberCheckBox);

    QHBoxLayout *buttonLayout1 = new QHBoxLayout;
    buttonLayout1->addWidget(forgotPasswordButton);
    buttonLayout1->addWidget(registerButton);

    QHBoxLayout *buttonLayout2 = new QHBoxLayout;
    buttonLayout2->addWidget(loginButton);
    buttonLayout2->addWidget(exitButton);

    mainLayout->addLayout(buttonLayout1);
    mainLayout->addLayout(buttonLayout2);

    // 信号与槽连接
    connect(loginButton, &QPushButton::clicked, this, &LoginWidget::onLoginButtonClicked);
    connect(exitButton, &QPushButton::clicked, this, &LoginWidget::onExitButtonClicked);
    connect(registerButton, &QPushButton::clicked, this, &LoginWidget::onRegisterButtonClicked);
    connect(forgotPasswordButton, &QPushButton::clicked, this, &LoginWidget::onForgotPasswordButtonClicked);
    connect(socket, &QTcpSocket::readyRead, this, &LoginWidget::readServerResponse);

    // 连接服务器
    connectToServer();
}

LoginWidget::~LoginWidget()
{
    delete ui;
}


void LoginWidget::onLoginButtonClicked()
{
    QString username = userLineEdit->text();
    QString password = passwordLineEdit->text();

    if (username.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this, "Input Error", "Please enter both username and password.");
        return;
    }

    QByteArray cayptd_pwd = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Sha512);
    QString user_part, passwd_part;
    passwd_part = cayptd_pwd.toHex().data();
    user_part = username.toLatin1().data();
    QString request = "LOGIN:" + user_part + ":" + passwd_part;

    //QString request = "LOGIN:" + username + ":" + password;
    sendRequest(request);
}

void LoginWidget::onExitButtonClicked()
{
    close();
}

void LoginWidget::onRegisterButtonClicked()
{

    RegisterWidget *registerWindow = new RegisterWidget();
    //registerWindow->exec();
    registerWindow->show();
    //this->hide();
}

void LoginWidget::onForgotPasswordButtonClicked()
{
    QMessageBox::information(this, "找回密码", "跳转到找回密码页面...");
}

void LoginWidget::connectToServer()
{
    socket->connectToHost("10.11.9.30 ", 2580);  // 连接到服务器 (根据实际情况调整IP和端口)
    if (!socket->waitForConnected(100)) {
        QMessageBox::warning(this, "连接错误", "无法连接到服务器");
        return;
    }

}

void LoginWidget::sendRequest(const QString &request)
{
    if (socket->state() == QAbstractSocket::ConnectedState) {
        socket->write(request.toUtf8());  // 发送请求
    } else {
        QMessageBox::warning(this, "Connection Error", "Unable to connect to the server.");
    }
}

void LoginWidget::readServerResponse()
{
    QByteArray response = socket->readAll();
    QString responseStr = QString::fromUtf8(response);

    // 根据服务器响应显示不同的提示信息
    if (responseStr == "ALREADY RGS") {
        QMessageBox::information(this, "注册", "该用户名已被注册，请选择其他用户名。");
    } else if (responseStr == "SUCCESS") {
        QMessageBox::information(this, "注册", "注册成功！");
    } else if (responseStr == "LOGIN SUCCESS") {
        QMessageBox::information(this, "登录", "登录成功！");
    } else if (responseStr == "LOGIN FAILED") {
        QMessageBox::information(this, "登录", "登录失败，用户名或密码错误。");
    } else {
        QMessageBox::information(this, "Error", "未知响应: " + responseStr);
    }
}
